<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
	google.load("jquery", "1.5.1");
	google.load("visualization", "1", {packages:["corechart"]});
</script>
<script type="text/javascript">
	<!--
	$(document).ready(function() {
		$('.timelist a').click(function() {
			Counter.stop();
			if ( $(this).hasClass('active') )
			{
				$(this).removeClass('active');
			}
			else
			{
				$('.timelist a.active').removeClass('active');
				$(this).addClass('active');
				Counter.start( $(this).parent().attr('rel') );
			}
		});

		Counter.count();
	});

	var Counter = {
		current: null,
		times: {},
		start: function(type) {
			if ( Counter.current ) Counter.stop(Counter.current);
			Counter.current = type;
		},
		stop: function() {
			Counter.current = null;
		},

		count: function( count ) {
			if ( Counter.current && count )
			{
				if ( !Counter.times[Counter.current] ) Counter.times[Counter.current] = 0;
				Counter.times[Counter.current] += 1;

				Counter.draw();
			}
			
			setTimeout('Counter.count(true)', 1000);
		},

		draw: function()
		{
			var time = Counter.formatTime( Counter.times[Counter.current] );
			$('.timelist li[rel=' + Counter.current + '] b').html( time );

			var total = 0;
			for ( var type in Counter.times ) total += Counter.times[type];

			$('.timelist li.total b').html( Counter.formatTime(total) );
			Counter.graph();
		},

		formatTime: function( time )
		{
			var hours = Math.floor(time / 3600);
			var minutes = Math.floor((time - hours*3600)/60);
			var seconds = time - hours*3600 - minutes*60;

			if ( hours > 0 )
				return hours + ' ч. ' + minutes + ' м. ' + seconds + ' с.';

			if ( minutes > 0 )
				return minutes + ' м. ' + seconds + ' с.';

			return seconds + ' с.';
		},

		chart: null,
		graph: function()
		{
			var data = new google.visualization.DataTable();
			data.addColumn('string', 'Деятельность');
			data.addColumn('number', 'Время');
			data.addRows(4);

			data.setValue(0, 0, 'Перекуры');
			data.setValue(0, 1, Counter.times.smoke);
			data.setValue(1, 0, 'Хавка');
			data.setValue(1, 1, Counter.times.eat);
			data.setValue(2, 0, 'Телефон');
			data.setValue(2, 1, Counter.times.phone);
			data.setValue(3, 0, 'Игрушки');
			data.setValue(3, 1, Counter.times.games);

			if ( !Counter.chart )
				Counter.chart = new google.visualization.PieChart(document.getElementById('graph'));
			
			Counter.chart.draw(data, {
				width: 350,
				height: 350,
				colors: ['#3366CC', '#DC3912', '#FF9900', '#990099'],
				legend: 'none'
			});
		}
	}
	//-->
</script>